﻿using System.Data.Common;
using System.Transactions;
using SpectrumAnalyser.Data.Common.Contracts;

namespace SpectrumAnalyser.Data.Common
{
    public class TransactionWrapper : ITransaction
    {
        private readonly TransactionScope _transaction;
        private readonly DbManagerBase _dbManagerBase;
        private bool _completed;

        public TransactionWrapper(TransactionScope transaction, DbManagerBase dbManagerBase)
        {
            //TransactionScope
            _transaction = transaction;
            _dbManagerBase = dbManagerBase;
        }

        public void Commit()
        {
            _transaction.Complete();
            _completed = true;
        }

        public void RollBack()
        {
            _transaction.Dispose();
            _dbManagerBase.Dispose();
            _completed = false;
        }

        public void Dispose()
        {
            if (_completed)
            {
                _transaction.Dispose();
                _dbManagerBase.Dispose();
            }
        }
    }
}